package com.lsz.crm.controller;

import com.lsz.crm.base.BaseController;
import com.lsz.crm.base.ResultInfo;
import com.lsz.crm.pojo.Role;
import com.lsz.crm.query.RoleQuery;
import com.lsz.crm.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Author liushizhong
 * @Date 2021/10/28 21:05
 * @Version 1.0
 */

@Controller
@RequestMapping("role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    /**
     * 查询⻆⾊列表
     *
     * @return
     */
    @RequestMapping("queryAllRoles")
    @ResponseBody
    public List<Map<String, Object>> queryAllRoles(Integer userId) {
        return roleService.queryAllRoles(userId);
    }

    /**
     * 转向角色页
     * @return
     */
    @RequestMapping("index")
    public String index() {

        return "role/role";
    }

    /**
     * 展示用户角色列表
     * @param roleQuery
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> userList(RoleQuery roleQuery) {
        return roleService.queryByParamsForTable(roleQuery);
    }

    /**
     * 跳转角色添加或修改页
     * @param roleId
     * @param model
     * @return
     */
    @RequestMapping("toAddOrUpdateRolePage")
    public String toAddAndUpdatePage(Integer roleId, Model model){
        if(null != roleId){
            // 更新操作
            model.addAttribute("role",roleService.selectByPrimaryKey(roleId));
        }
        return "role/addAndUpdateRole";
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public ResultInfo saveRole(Role role){

        roleService.addRole(role);
        return success("⻆⾊记录添加成功");
    }

    /**
     * 更新角色
     * @param role
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateRole(Role role){

        roleService.updateRole(role);
        return success("⻆⾊记录更新成功");
    }

    /**
     * 删除角色
     * @param roleIds
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteRole(Integer[] roleIds){

        roleService.deleteRole(roleIds);
        return success("⻆⾊记录删除成功");
    }

    /**
     * 角色授权
     * @param roleId
     * @param ids
     * @return
     */
    @PostMapping("addGrant")
    @ResponseBody
    public ResultInfo addGrant(Integer roleId,Integer[] ids){
        roleService.addGrant(roleId,ids);
        return success("授权成功");
    }
}
